require 'rubygems'
require 'active_record'
Dir.glob('../db/models/*.rb').each {|f| require f }




ActiveRecord::Base.establish_connection(
      :adapter => "sqlite3",
      :dbfile  => "../var/yabzs.sqlite"
 )





#tyle minut ze ile kill eventow/ile osob
Dir.glob('logs/*.txt').each do |log_file|
p log_file

server_start = nil
game = nil
players = []
event_datetime = nil
flags = false

File.open(log_file).each do |line|
	if line =~ /------------ SERVER START \[ (\d\d\d\d-\d\d-\d\d) (\d\d?-\d\d?-\d\d?) \] ------------/:
		server_start = DateTime.parse($1 + " " + $2.gsub(/-/,":"))
		event_datetime = server_start
	elsif line =~ /\*\*\* '(.+)' joined the game as (.+)\./:
		players.push $1
	elsif line =~ /\*\*\* Game started\./:
		game = DB::Game.create :datetime => server_start
		players.each { |player| DB::Player.find_or_create_by_name(player) }
		
	elsif line =~ /\*\*\* '(.+)' blew myself up\./:
		suicide = DB::EventType.find_by_name('suicide')
		p = DB::Player.find_by_name($1)
		event_datetime = event_datetime.advance(:seconds => 30/players.length)
		game.events.create(:event_type => suicide, :player => p, :target => p, :datetime => event_datetime)
	elsif line =~ /\*\*\* '(.+)' destroyed by '(.+)' who carried -(.+)- flag\./:
		kill = DB::EventType.find_by_name('kill')
		player = DB::Player.find_by_name($2)
		target = DB::Player.find_by_name($1)
		if $3 != 'no':
			flags = true
		end
		flag = DB::Flag.find_by_acronym($3)
		event_datetime = event_datetime.advance(:seconds => 30/players.length)
		game.events.create(:event_type => kill, :player => player, :target => target, :datetime => event_datetime, :flag => flag)
	elsif line =~ /\*\*\* Game ended\./:
		game.flags = flags	
		game.duration = event_datetime.to_time - server_start.to_time
		game.save
	end
	
end
end



